package com.hanhai.zrb.api.mongodb.demo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.hanhai.zrb.api.mongodb.BeanUtil;
import com.hanhai.zrb.api.mongodb.MongoUtil;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;

public class MongoDBDemoAdvance {

	public static final String TITLE = "title";
	public static final String CONTENT = "content";
	public static final String AUTHOR = "author";
	public static final String DATE = "date";
	public static final String ID = "id";

	//构造1个文章、修改之后再存、2个查询
	public static void main(String[] args) {

		Mongo mongo = MongoUtil.mongo();
		DB db = mongo.getDB("blog");

		Article article = buildArticle();
		DBCollection articleCollection = add(db, article);

		article.setId(2L);
		DBObject object2 = articleToObject2(article);
		articleCollection.insert(object2);

		Long id = 1L;
		Article articleBefore = findById(articleCollection, id);
		if (articleBefore != null) {
			System.out.println("刚刚新增的文章=" + articleBefore);
		}

		articleBefore.setTitle("标题被修改啦");
		update(articleCollection, articleBefore);
		Article articleAfter = findById(articleCollection, id);
		if (articleAfter != null) {
			System.out.println("修改后的文章=" + articleAfter);
		}

		List idList = Arrays.asList(1L, 2L);
		List<Article> articleList = findByIdList(articleCollection, idList);
		if (articleList != null) {
			for (Article a : articleList) {
				System.out.println(a);
			}
		}

		BasicDBObject removeAll = new BasicDBObject();
		articleCollection.remove(removeAll);
	}

	//增加
	public static DBCollection add(DB db, Article article) {
		DBObject object = articleToObject2(article);
		DBCollection articleCollection = db.getCollection("article");
		articleCollection.insert(object);
		return articleCollection;
	}

	//构造测试数据
	private static Article buildArticle() {
		Article article = new Article();
		article.setAuthor("FansUnion");
		article.setContent("neirong");
		article.setDate(new Date());
		article.setId(1L);
		article.setTitle("title");
		return article;
	}

	// 从集合中，根据ID查找
	public static Article findById(DBCollection collection, Long id) {
		BasicDBObject searchArticleById = new BasicDBObject();
		searchArticleById.append(ID, id);
		Article articleBefore = null;
		DBCursor cursor = collection.find(searchArticleById);
		while (cursor.hasNext()) {
			DBObject articleObject = cursor.next();
			articleBefore = objectToArticle(articleObject);

			String internalId = articleObject.get("_id").toString();
			articleBefore.setInternalId(internalId);
		}
		cursor.close();
		return articleBefore;
	}

	// 修改
	public static void update(DBCollection collection, Article article) {
		if (article.getId() == null) {
			return;
		}

		BasicDBObject updateCondition = new BasicDBObject();
		updateCondition.append(ID, article.getId());

		DBObject newObject = BeanUtil.bean2DBObject(article);

		DBObject updateSetValue = new BasicDBObject("$set", newObject);
		collection.update(updateCondition, updateSetValue);
	}

	//根据ID集合查找
	public static List<Article> findByIdList(DBCollection collection,
			List<Long> idList) {
		BasicDBList values = new BasicDBList();
		values.addAll(idList);

		DBObject inQuery = new BasicDBObject("$in", values);

		DBObject con = new BasicDBObject();
		con.put(ID, inQuery);
		DBCursor cursorIdArray = collection.find(con);

		List<Article> articleList = new ArrayList<Article>();
		while (cursorIdArray.hasNext()) {
			DBObject articleObject = cursorIdArray.next();
			Article article = new Article();
			BeanUtil.dbObject2Bean(articleObject, article);
			String internalId = articleObject.get("_id").toString();
			article.setInternalId(internalId);

			articleList.add(article);
		}
		return articleList;
	}

	//对象转换
	private static Article objectToArticle(DBObject object) {
		if (object == null) {
			return null;
		}
		Article article = new Article();
		// 用工具方法转换，手动转换，需要判断类型，比较麻烦
		article = BeanUtil.dbObject2Bean(object, article);
		return article;
	}

	// 手动把Bean转换为Mongodb的对象
	private static BasicDBObject articleToObject(Article article) {
		BasicDBObject object = new BasicDBObject();
		object.append(TITLE, article.getTitle());
		object.append(CONTENT, article.getContent());
		object.append(AUTHOR, article.getAuthor());
		object.append(DATE, article.getDate());
		object.append(ID, article.getId());
		return object;
	}

	// 实用工具转换，简单
	private static DBObject articleToObject2(Article article) {
		return BeanUtil.bean2DBObject(article);
	}
}
